<?php
######################################################################################
# XC SEO :: http://code.google.com/p/x-cart-seo/
# Description: This is an SEO optimizing module for the X-Cart shopping cart
# Author: PHP Site Solutions (intel352 aka Jon Langevin)
# Website: http://www.phpsitesolutions.com
# License: http://www.gnu.org/licenses/gpl.html GPL version 3 or any later version
#######################################################################################

if ( !defined('XCART_START') ) { header('Location: ../'); die('Access denied'); }

#
# Below, specify your CDSEO configurations
#
$cdseo_cat	= 'category';
$cdseo_prod = 'product';
$cdseo_pages= 'pages';
$cdseo_manu = 'manufacturer';

$cdseo_array = array();
$cdseo_array[] = array($xcart_web_dir . '/' . $cdseo_cat, 'cat');
$cdseo_array[] = array($xcart_web_dir . '/' . $cdseo_prod, 'productid');
$cdseo_array[] = array($xcart_web_dir . '/' . $cdseo_pages, 'pageid');
$cdseo_array[] = array($xcart_web_dir . '/' . $cdseo_manu, 'manufacturerid');

foreach($cdseo_array AS $v) {
	if(	($strpos = strpos($seo_path, $v[0]))!==false ) {
		$cdseo_file = substr($seo_path, $strpos + strlen($v[0]));
		$cdseo_file = ltrim( rtrim($cdseo_file, '/'), '/');

		if(class_exists('seo_filter')) {
			$seo = new seo_filter;

			$seo_qs = array();
			foreach($_GET AS $kk=>$vv){
				if($kk!=$XCART_SESSION_NAME)
					$seo_qs[] = $kk.'='.$vv;
			}

			switch($v[1]) {
				case 'cat':
					$id = func_query_first_cell('SELECT categoryid FROM ' . $sql_tbl['categories'] . ' WHERE cdseo_url!="" AND cdseo_url="' . mysql_real_escape_string($cdseo_file) . '"');
					$callback = '_category_callback';
				break;
				case 'productid':
					$id = func_query_first_cell('SELECT productid FROM ' . $sql_tbl['products'] . ' WHERE cdseo_url!="" AND cdseo_url="' . mysql_real_escape_string($cdseo_file) . '"');
					$callback = '_product_callback';
				break;
				case 'pageid':
					$id = func_query_first_cell('SELECT pageid FROM ' . $sql_tbl['pages'] . ' WHERE filename="' . mysql_real_escape_string($cdseo_file) . '"');
					$callback = '_pages_callback';
				break;
				case 'manufacturerid':
					$id = func_query_first_cell('SELECT manufacturerid FROM ' . $sql_tbl['manufacturers'] . ' WHERE cdseo_url!="" AND cdseo_url="' . mysql_real_escape_string($cdseo_file) . '"');
					$callback = '_manufacturer_callback';
				break;
			}
			$seo_qs[] = $v[1].'='.$id;
			$seo_qs = array(false,'','',implode('&',$seo_qs),'','');

			$seo_url = $xcart_web_dir.'/'.$seo->$callback($seo_qs, true);

			if($seo_url && $seo_url!=$_SERVER['REQUEST_URI']){
				header( 'HTTP/1.1 301 Moved Permanently' );
				func_header_location($seo_url);
				exit;
			}
		}
	}
}

?>